//
//  DeliverDetailViewController.m
//  at_xiaoge_client
//
//  Created by Linyu Niu on 16/03/2018.
//  Copyright © 2018 Linyu Niu. All rights reserved.
//

#import "DeliverDetailViewController.h"

@interface DeliverDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView * detailTableView;
    NSDictionary * expressData;
}
@end

@implementation DeliverDetailViewController
@synthesize expressNumber;
@synthesize courierID,isRegister;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    __weak DeliverDetailViewController * weakSelf = self;
    
    //
    
    UIView * navView = [[CTMediator sharedInstance] navgationHeadViewInTargetView:self.view params:[NSDictionary dictionaryWithObjectsAndKeys:@"快递员身份验证",@"title",@"share",@"rightBtnImage", nil] leftAction:^(UIButton * sender){
        [weakSelf.navigationController popViewControllerAnimated:YES];
    } rightAction:^(UIButton * sender){
        
    }];
    
    
    
    UIView * bottomView = [[UIView alloc] init];
    bottomView.frame = CGRectMake(0,self.view.frame.size.height - 100,self.view.frame.size.width,100);
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    
    UIView * topLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, bottomView.frame.size.width, 1.0)];
    topLineView.backgroundColor = [UIColor colorWithRed:77.0 / 255.0 green:94.0 / 255.0 blue:107.0 / 255.0 alpha:0.5];
    [bottomView addSubview:topLineView];
    
    
    UILabel * desLabel = [[UILabel alloc] init];
    desLabel.frame = CGRectMake(20,10,bottomView.frame.size.width - 40,16);
    desLabel.text = @"请确认是否出现xxx或者手机号xxxxxxxxxxx 正在派件的字样";
    desLabel.font = [UIFont systemFontOfSize:14];
    desLabel.textColor = [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0];
    desLabel.numberOfLines = 0;
    [desLabel enlargeHeightToKeepFontSize];
    [bottomView addSubview:desLabel];
    
    
    UIButton * inputButton = [UIButton buttonWithType:UIButtonTypeCustom];
    inputButton.frame = CGRectMake(0,bottomView.frame.size.height - 40,bottomView.frame.size.width / 2,40);
    inputButton.backgroundColor = [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0];
    [inputButton setTitle:@"重新输入" forState:UIControlStateNormal];
    inputButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [inputButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [inputButton addTarget:self action:@selector(inputNumber) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:inputButton];
    
    UIButton * confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    confirmButton.frame = CGRectMake(inputButton.frame.size.width,bottomView.frame.size.height - 40,bottomView.frame.size.width / 2,40);
    [confirmButton addTarget:self action:@selector(confirm) forControlEvents:UIControlEventTouchUpInside];
    confirmButton.backgroundColor = UIColorFromRGB(0x20C67A);
    [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
    confirmButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [bottomView addSubview:confirmButton];
    
    
    detailTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, navView.frame.origin.y + navView.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - navView.frame.size.height - navView.frame.origin.y - bottomView.frame.size.height)];
    detailTableView.delegate = self;
    detailTableView.dataSource = self;
    detailTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:detailTableView];
    [self getData];
}
-(void)confirm{
    NSArray * logisticsInfo = [expressData objectForKey:@"list"];
    if([logisticsInfo count]){
        NSDictionary * dic = [logisticsInfo firstObject];
        [self startLoadingView:self.view];
        __weak DeliverDetailViewController * weakSelf = self;
        [[CTMediator sharedInstance] bindCourierExpressNo:courierID registIs:isRegister expressNO:expressNumber expressId:[dic objectForKey:@"expressId"] block:^(NSDictionary * dic){
            [weakSelf stopLoadingView:weakSelf.view needShowMsg:YES result:dic];
            if([weakSelf isSuccess:dic]){
                [self dismissViewControllerAnimated:YES completion:nil];
            }
        }];
    }
    
}
-(void)inputNumber{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)getData{

    [self startLoadingView:self.view];
    __weak DeliverDetailViewController * weskSelf = self;
    
    [[CTMediator sharedInstance] getCourierLatestExpressByNumber:expressNumber block:^(NSDictionary * dic){
        expressData = dic;
        NSArray * logisticsInfo = [expressData objectForKey:@"list"];
        
        [detailTableView reloadData];
        [weskSelf stopLoadingView:self.view needShowMsg:NO result:dic];
        [weskSelf addEmptyView:detailTableView list:logisticsInfo imgName:@"deliverable_empty"];
    }];
}

-(void)createExpressInfoCell:(ElementTableViewCell *)cell{
    UIImageView * imgView = [[UIImageView alloc]initWithFrame:CGRectMake(30, 20, 44, 44)];
    imgView.layer.cornerRadius = 22.0;
    imgView.layer.borderColor = UIColorFromRGB(0x979797).CGColor;
    imgView.layer.borderWidth = 0.5;
    imgView.layer.masksToBounds = YES;
    [cell addSubview:imgView];
    
    
    NSString * url = [expressData objectForKey:@"expressLog"];
    [imgView sd_setImageWithURL:[NSURL URLWithString:url]];
    UILabel * nameLabel = [[UILabel alloc] init];
    nameLabel.frame = CGRectMake(imgView.frame.origin.x + imgView.frame.size.width + 20, imgView.frame.origin.y, detailTableView.frame.size.width, 28);
    nameLabel.text = [expressData objectForKey:@"expressName"];
    nameLabel.font = [UIFont systemFontOfSize:18];
    nameLabel.textColor = [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0];
    [cell addSubview:nameLabel];
    
    UILabel * numLabel = [[UILabel alloc] init];
    numLabel.text = [expressData objectForKey:@"expressNo"];
    numLabel.frame = CGRectMake(nameLabel.frame.origin.x, nameLabel.frame.size.height + nameLabel.frame.origin.y + 3, 0, 20);
    numLabel.font = [UIFont systemFontOfSize:12];
    numLabel.textColor = [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:0.48];
    [numLabel enlargeWidthToKeepFontSize];
    [cell addSubview:numLabel];
    
    UIButton * copyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    copyButton.layer.cornerRadius = 3.0;
    copyButton.layer.borderWidth = 0.5;
    copyButton.layer.borderColor = UIColorFromRGBWithAlpha(0x333333, 0.5).CGColor;
    copyButton.titleLabel.font = [UIFont systemFontOfSize:10];
    [copyButton setTitleColor:[UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:0.5] forState:UIControlStateNormal];
    [copyButton addTarget:self action:@selector(copyNumber) forControlEvents:UIControlEventTouchUpInside];
    copyButton.frame = CGRectMake(numLabel.frame.origin.x + numLabel.frame.size.width + 10, numLabel.frame.origin.y, 43, numLabel.frame.size.height);
    [copyButton setTitle:@"复制" forState:UIControlStateNormal];
//    [cell addSubview:copyButton];
    cell.cellHeight = imgView.frame.origin.y + imgView.frame.size.height + 20;
}
-(void)copyNumber{
    NSString * number = [expressData objectForKey:@"expressNo"];
    UIPasteboard * pasteboard = [UIPasteboard generalPasteboard];
    
    
    if (number && ![@"" isEqualToString:number]) {
        [pasteboard setString:number];
    }
    [MBProgressHUD wj_showSuccess:@"复制成功"];
}
-(void)createItemCell:(ElementTableViewCell *)cell index:(NSIndexPath *)indexPath{
    NSArray * array = [expressData objectForKey:@"list"];
    NSDictionary * dic = [array objectAtIndex:[indexPath row]];
    
    UIView * dotView = [[UIView alloc] init];
    dotView.layer.cornerRadius = 4;
    dotView.frame = CGRectMake(33, 5, 8, 8);
    dotView.backgroundColor = [UIColor colorWithRed:32.0/255.0 green:198.0/255.0 blue:122.0/255.0 alpha:1.0];
    [cell addSubview:dotView];
    UILabel * statusLabel = [[UILabel alloc]initWithFrame:CGRectMake(dotView.frame.origin.x + dotView.frame.size.width + 15, dotView.frame.origin.y - 6, detailTableView.frame.size.width - dotView.frame.size.width - dotView.frame.origin.x - 25, 20)];
    statusLabel.numberOfLines = 0;
    statusLabel.font = [UIFont systemFontOfSize:13];
    [cell addSubview:statusLabel];
    
    UILabel * contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(dotView.frame.origin.x + dotView.frame.size.width + 15, statusLabel.frame.origin.y + statusLabel.frame.size.height + 5, statusLabel.frame.size.width, 0)];
    contentLabel.font = [UIFont systemFontOfSize:14];
    contentLabel.numberOfLines = 0;
    [cell addSubview:contentLabel];
    
    
    NSMutableString * string = [NSMutableString string];
    [string appendString:[dic objectForKey:@"info"]];
//    NSString * statusCH = [dic objectForKey:@"statusCH"];
//    if (statusCH && ![@"" isEqualToString:statusCH]) {
//        [string appendString:statusCH];
//        [string appendString:@"\n"];
//    }
//    [string appendString:[dic objectForKey:@"infoTime"]];
//    [string appendString:@"  "];
//    [string appendString:[dic objectForKey:@"title"]];
//    [string appendString:@"  "];
//    [string appendString:[dic objectForKey:@"lastInfo"]];
    
    [statusLabel setLineSpacing:5.0 labelText:string];
    
    
    [statusLabel enlargeHeightToKeepFontSize];
    
    cell.cellHeight = statusLabel.frame.origin.y * 2 + statusLabel.frame.size.height + 25;
    
    UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(dotView.frame.origin.x + (dotView.frame.size.width - 1) / 2, dotView.frame.origin.y + dotView.frame.size.height + 5, 1.0, cell.cellHeight - dotView.frame.size.height - dotView.frame.origin.y - 5)];
    lineView.backgroundColor = UIColorFromRGBWithAlpha(0xE9E9E9, 1.0);
    [cell addSubview:lineView];
    if ([indexPath row] == 0) {
        dotView.backgroundColor = UIColorFromRGB(0x20C67A);
        statusLabel.textColor = UIColorFromRGB(0x20C67A);
        contentLabel.textColor = UIColorFromRGB(0x20C67A);
    }else{
        dotView.backgroundColor = UIColorFromRGBWithAlpha(0x333333, 0.5);
        statusLabel.textColor = UIColorFromRGB(0x333333);
        contentLabel.textColor = UIColorFromRGB(0x333333);
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else if(section == 1){
        NSArray * array = [expressData objectForKey:@"list"];
        return [array count];
    }
    return 0;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if ([expressData count]) {
        return 2;
    }
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ElementTableViewCell * cell = [[ElementTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    if([expressData count]){
        if ([indexPath section] == 0) {
            [self createExpressInfoCell:cell];
        }else{
            [self createItemCell:cell index:indexPath];
        }
    }
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ElementTableViewCell * cell = (ElementTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.cellHeight;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        UIView * v = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 60)];
        v.backgroundColor = [UIColor whiteColor];
        UILabel * label = [[UILabel alloc] init];
        label.frame = CGRectMake(0, 0, tableView.frame.size.width, 38);
        label.backgroundColor = [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:0.04];
        label.textColor = UIColorFromRGBWithAlpha(0x333333, 0.5);
        label.text = @"物流信息";
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:15];
        [v addSubview:label];
        return v;
    }
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return 60;
    }
    return 0;
}

@end
